package xpn.platform.modules.workflow.definition.process;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.webmvc.PersistentEntityResource;
import org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.data.rest.webmvc.ResourceNotFoundException;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.hateoas.Link;
import org.springframework.hateoas.PagedModel;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import xpn.platform.common.config.XpnPlatformConfig;
import xpn.platform.common.repository.XpnRepositoryController;
import xpn.platform.modules.todo.donelink.DoneLink;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 自定义Spring Data Rest不能自动实现的RESTful接口
 * 
 * @author bobatkm
 * @version 2017年8月31日 下午4:02:10
 */

@RepositoryRestController
@RestController
@RequestMapping("workflow_definition_processes")
public class ProcessController extends XpnRepositoryController<DoneLink, Long> {
	@Autowired
	private ProcessDAO processDefinitionDao;

	@SuppressWarnings("rawtypes")
	@Autowired
	private PagedResourcesAssembler pagedResourcesAssembler;

	/**
	 * 返回分页数据
	 */
	@SuppressWarnings({ "unchecked" })
	@GetMapping(value = "testPage")
	public PagedModel<java.lang.Process> findAll(Pageable param, PersistentEntityResourceAssembler resourceAssembler) {
		Page<java.lang.Process> page = processDefinitionDao.findAll(param);
		return pagedResourcesAssembler.toModel(page, resourceAssembler);
	}

	/**
	 * 测试返回单个实体
	 */
	@GetMapping(value = "testGet/{id}")
	public PersistentEntityResource getOne(@PathVariable Long id, PersistentEntityResourceAssembler resourceAssembler) {
		java.lang.Process entity = processDefinitionDao.getOne(id);
		if (entity == null) {
			throw new ResourceNotFoundException("Resource not found!");
		}
		return resourceAssembler.toFullResource(entity);
	}

	/**
	 * 测试保存功能
	 */
	@PostMapping(value = "testSave")
	@RequiresPermissions("workflow.admin")
	public PersistentEntityResource testSave(@Valid @RequestBody java.lang.Process entity,
			PersistentEntityResourceAssembler resourceAssembler) {
		return resourceAssembler.toFullResource(processDefinitionDao.save(entity));
	}

	/**
	 * 测试更新功能
	 */
	@PatchMapping(value = "testPatch/{id}")
	@RequiresPermissions("workflow.admin")
	public PersistentEntityResource testPatch(@Valid @RequestBody java.lang.Process newEntity, @PathVariable Long id,
                                              PersistentEntityResourceAssembler resourceAssembler) {
		java.lang.Process entity = processDefinitionDao.getOne(id);
		newEntity.setId(entity.getId());
		return resourceAssembler.toFullResource(processDefinitionDao.save(newEntity));
	}

	/*
	 * 测试通过RESTful接口读取远程对象
	 */
	@Autowired
	private RestTemplate restTemplate;

	@Autowired
	XpnPlatformConfig xpnPlatformConfig;

	@GetMapping(value = "testRestService/{id}")
	public Object restGetOne(@PathVariable Long id, HttpServletRequest httpRequest,
			PersistentEntityResourceAssembler resourceAssembler) {
		java.lang.Process entity = processDefinitionDao.getOne(id);
		PersistentEntityResource resource = resourceAssembler.toFullResource(entity);

		String href = resource.getLink("self").get().getHref();
		String xpnTokenName = xpnPlatformConfig.getSecurity().getAuthenticationTokenName();
		String token = httpRequest.getHeader(xpnTokenName);
		href += "?" + xpnTokenName + "=" + token;

		// 远程获取对象
		Object restEntity = restTemplate.getForObject(href, Object.class);
		//restEntity.setId(id); // 不设置无法组装为rest格式的返回
		return restEntity;
	}
}